﻿
using System;
using System.Collections.Generic;
using CarTrackr.Business.Validation;
using CarTrackr.Data.Attributes;
 


namespace CarTrackr.Business
{
    public class Car :BusinessBase 
    {
           [DbField(Name="Id")]
           public Guid    Id { get; set; }

           [DbField(Name = "Make")]
           public string  Make { get; set;}

           [DbField(Name = "Model")]
           public string  Model { get; set;}

           [DbField(Name = "PurchasePrice")]
           public decimal PurchasePrice { get; set;}

           [DbField(Name = "LicensePlate")]
           public string  LicensePlate { get; set;}

           [DbField(Name = "FuelType")]
           public string  FuelType { get; set;}

           [DbField(Name = "Description")]
           public string  Description { get; set;}


          public decimal TotalKilometers
          {
             get
             {
                 decimal total = 0;

                 foreach(Refuelling refuelling in Refuellings)
                 {
                     total += refuelling.Kilometers;

                 }
                 return total; 
             }
          }

        public decimal AverageUsage
        {

            get
            {
                int count = 0;

                decimal sum = 0;
                foreach (Refuelling refuelling in Refuellings)
                {
                    sum += refuelling.Usage;
                    count++;

                }

                if (count == 0) return 0;

                return sum/count;
            }

        }


        public decimal TotalCost
        {

            get
            {


                decimal totalCost = 0;
                foreach (Refuelling refuelling in Refuellings)
                {
                    totalCost += refuelling.PricePerLiter*refuelling.Liters;  
                   
                }

                return totalCost; 
               
            }

        }

           
        public decimal AverageCost 
        {
            get
            {

                if (TotalKilometers == 0) return 0;

                return TotalCost/TotalKilometers;

            }
        }


        public Refuelling LastRefuelling
        {
           get
           {
                if (Refuellings.Count==0) return null;

               return Refuellings[0];

           }
        }
           
        public List<Refuelling> Refuellings;

        public Car() 
        {
            Refuellings = new List<Refuelling>();

            CreateRules();
            
        }

        public void AddRefuelling(Refuelling refuelling)
        {
                Refuellings.Add(refuelling);
        }



        public void  CreateRules()
        {

             AddRule(new RequiredRule {PropertyName = "Make"});
             AddRule(new RequiredRule {PropertyName = "Model"});
             AddRule(new AboveZeroRule   {PropertyName = "PurchasePrice"});
             AddRule(new RequiredRule {PropertyName = "LicensePlate"});
             
          

        }

    }
}

 